import React from 'react';
import { inject, observer, Provider } from 'mobx-react';
import * as mobx from 'mobx';
import { WeaTools, WeaErrorPage, WeaLocaleProvider } from 'ecCom';

import OdocTransferDialog from '../components/odocRequest/index';
import * as dialogTypes from '../constants/OdocCompsDialogType';
import OdocWithdraw from '../components/odocRequestWithdraw/index';
import OdocWithdrawManager from '../components/odocRequestWithdraw/managerindex';
import OdocUpdateOperator from '../components/odocRequestUpdateOperator';
import OdocFillRemark from './OdocMenuOperate/OdocFillRemark';
import OdocTypeChangeDialog from './OdocTypeChangeDialog';
import OdocAppend from '../components/signlog/append/append';
import OdocAppendSelf from '../components/signlog/append/appendSelf';
import OdocSignUpdate from '../components/signlog/update/index';
import OdocAdjustLog from '../components/odocRequestAdjustLog/adjustLog';
import stores from '../stores/index';

const { getLocaleLabelByCode, getLabel } = WeaLocaleProvider;

@inject('odocCompsStore')
@observer
class OdocComps extends React.Component {
  constructor(props) {
    super(props);
    this.closeDialog = this.closeDialog.bind(this);
  }

  closeDialog(key, res) {
    const { odocCompsStore } = this.props;

    // 执行窗口关闭时的事件
    let { dialogParam } = odocCompsStore;
    dialogParam = mobx.toJS(dialogParam);
    const param = dialogParam[key];
    if (param && param.onClose && typeof param.onClose == 'function') {
      if (key === dialogTypes.ODOC_APPEND_SIGNLOG ||
        key === dialogTypes.ODOC_APPEND_SIGNLOG_SELF ||
        key === dialogTypes.ODOC_UPDATE_SIGNLOG ||
        key === dialogTypes.ODOC_WITHDRAW_DIALOG ||
        key === dialogTypes.ODOC_WITHDRAW_MANAGER_DIALOG
      ) {
        const { api_status = false } = res;
        param.onClose(api_status, res);
      } else {
        param.onClose();
      }
    }

    // 关闭窗口
    odocCompsStore.setDialogVisible(key, false);
  }

  renderDialog(key) {
    const { odocCompsStore } = this.props;
    const { dialogVisible, dialogInit } = odocCompsStore;
    let { dialogParam, requestParam } = odocCompsStore;
    dialogParam = mobx.toJS(dialogParam);
    requestParam = mobx.toJS(requestParam);

    let dialogComs = null;

    if (dialogInit.has(key) && dialogInit.get(key)) {
      if (key == dialogTypes.ODOC_TRANSFER_DIALOG) {
        const title = getLabel('518550', '公文流转');
        const param = dialogParam[key];
        const { requestid, workflowid, nodeid, markInfo = {} } = requestParam;
        const { remark } = markInfo;
        const dialogProps = {
          title, requestid, workflowid, nodeid, remark, ...param,
        };
        dialogComs = (<OdocTransferDialog ecId={`${this && this.props && this.props.ecId || ''}_OdocTransferDialog@ghs7ns`}
          {...dialogProps}
          visible={dialogVisible.get(key)}
          onClose={this.closeDialog.bind(this, key)}
        />);
      }
    }

    return dialogComs;
  }


  render() {
    const { odocCompsStore } = this.props;
    let { dialogInit, dialogVisible } = odocCompsStore;
    return (
      <div>
        {
          dialogInit.has(dialogTypes.ODOC_TRANSFER_DIALOG) && dialogInit.get(dialogTypes.ODOC_TRANSFER_DIALOG) && this.renderDialog(dialogTypes.ODOC_TRANSFER_DIALOG)
        }
        {
          dialogInit.has(dialogTypes.ODOC_WITHDRAW_DIALOG) && dialogInit.get(dialogTypes.ODOC_WITHDRAW_DIALOG) &&
          <OdocWithdraw ecId={`${this && this.props && this.props.ecId || ''}_OdocWithdraw@pifidu`} onClose={this.closeDialog.bind(this, dialogTypes.ODOC_WITHDRAW_DIALOG)} />
        }
        {
          dialogInit.has(dialogTypes.ODOC_WITHDRAW_MANAGER_DIALOG) && dialogInit.get(dialogTypes.ODOC_WITHDRAW_MANAGER_DIALOG) &&
          <OdocWithdrawManager ecId={`${this && this.props && this.props.ecId || ''}_OdocWithdrawManager@y5cnxk`}
            onClose={this.closeDialog.bind(this, dialogTypes.ODOC_WITHDRAW_MANAGER_DIALOG)}
          />
        }
        {
          dialogInit.has(dialogTypes.ODOC_UPDATEOPERATOR_DIALOG) && dialogInit.get(dialogTypes.ODOC_UPDATEOPERATOR_DIALOG) &&
          <OdocUpdateOperator ecId={`${this && this.props && this.props.ecId || ''}_OdocUpdateOperator@baz5pl`}
            dilogotherparams={{ subparams: { dialogType: 1 } }}
            onClose={this.closeDialog.bind(this, dialogTypes.ODOC_UPDATEOPERATOR_DIALOG)}
          />

        }

        {// 添加签字意见
          dialogInit.has(dialogTypes.ODOC_APPEND_SIGNLOG) && dialogInit.get(dialogTypes.ODOC_APPEND_SIGNLOG) &&
          <OdocAppend ecId={`${this && this.props && this.props.ecId || ''}_OdocAppend@932k7u`}
            visible={dialogVisible.get(dialogTypes.ODOC_APPEND_SIGNLOG)}
            onClose={(res = {}) => this.closeDialog(dialogTypes.ODOC_APPEND_SIGNLOG, res)}
          />
        }
        {// 添加签字意见(个人)
          dialogInit.has(dialogTypes.ODOC_APPEND_SIGNLOG_SELF) && dialogInit.get(dialogTypes.ODOC_APPEND_SIGNLOG_SELF) &&
          <OdocAppendSelf ecId={`${this && this.props && this.props.ecId || ''}_OdocAppendSelf@v4hk5t`}
            visible={dialogVisible.get(dialogTypes.ODOC_APPEND_SIGNLOG_SELF)}
            onClose={(res = {}) => this.closeDialog(dialogTypes.ODOC_APPEND_SIGNLOG_SELF, res)}
          />
        }

        {// 修改签字意见
          dialogInit.has(dialogTypes.ODOC_UPDATE_SIGNLOG) && dialogInit.get(dialogTypes.ODOC_UPDATE_SIGNLOG) &&
          <OdocSignUpdate ecId={`${this && this.props && this.props.ecId || ''}_OdocSignUpdate@s7451f`}
            visible={dialogVisible.get(dialogTypes.ODOC_UPDATE_SIGNLOG)}
            onClose={(res = {}) => this.closeDialog(dialogTypes.ODOC_UPDATE_SIGNLOG, res)}
          />
        }
        {// 填写意见
          dialogInit.has(dialogTypes.ODOC_FILL_REMARK_DIALOG) && dialogInit.get(dialogTypes.ODOC_FILL_REMARK_DIALOG) &&
          <OdocFillRemark ecId={`${this && this.props && this.props.ecId || ''}_OdocFillRemark@9flbt9`}
            onClose={this.closeDialog.bind(this, dialogTypes.ODOC_FILL_REMARK_DIALOG)}
          />
        }
        {// 转文
          dialogInit.has(dialogTypes.ODOC_CHANGE_ODOC_TYPE_DIALOG) && dialogInit.get(dialogTypes.ODOC_CHANGE_ODOC_TYPE_DIALOG) &&
          <OdocTypeChangeDialog ecId={`${this && this.props && this.props.ecId || ''}_OdocTypeChangeDialog@0lyr7c`}
            onClose={this.closeDialog.bind(this, dialogTypes.ODOC_CHANGE_ODOC_TYPE_DIALOG)}
          />
        }
        {// 调整日志
          dialogInit.has(dialogTypes.ODOC_ADJUST_LOG) && dialogInit.get(dialogTypes.ODOC_ADJUST_LOG) &&
          <OdocAdjustLog ecId={`${this && this.props && this.props.ecId || ''}_OdocAdjustLog@c2rhj6`}
            visible={dialogVisible.get(dialogTypes.ODOC_ADJUST_LOG)}
            onClose={this.closeDialog.bind(this, dialogTypes.ODOC_ADJUST_LOG)}
          />
        }
      </div>
    );
  }
}


class MyErrorHandler extends React.Component {
  render() {
    const hasErrorMsg = this.props.error && this.props.error !== '';
    return (
      <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@3pg8sg`} msg={hasErrorMsg ? this.props.error : `${getLabel(385218, '对不起，该页面异常，请联系管理员！')}`} />
    );
  }
}

class OdocComps2Export extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      reRender: false,
    };
  }

  componentDidMount() {
    getLocaleLabelByCode('odoc').then(() => this.setState({ reRender: true }));
  }

  render() {
    return (
      <Provider ecId={`${this && this.props && this.props.ecId || ''}_Provider@s3s7k4`} {...stores}>
        <OdocComps ecId={`${this && this.props && this.props.ecId || ''}_OdocComps@o1tx0d`} />
      </Provider>
    );
  }
}

export default WeaTools.tryCatch(React, MyErrorHandler, { error: '' })(OdocComps2Export);
